package com.foton.lotteryapplication.mq.producer;

import cn.hutool.json.JSONUtil;
import com.foton.lotterycommon.common.Constants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.support.SendResult;
import org.springframework.stereotype.Component;
import org.springframework.util.concurrent.ListenableFuture;
import org.springframework.util.concurrent.ListenableFutureCallback;

import javax.annotation.Resource;

/**
 * @Author RenPu
 * @Date 2024/1/8 17:44
 * @Version 1.0
 * @Description:
 **/

@Slf4j
@Component
public class LotteryKafkaProducer {

    @Resource
    private KafkaTemplate<String,Object> kafkaTemplate;


    /**
     * MQ主题：中奖发货单
     *
     * # linux 启动指令
     * 启动zk：bin/zookeeper-server-start.sh -daemon config/zookeeper.properties
     * 启动kafka：bin/kafka-server-start.sh -daemon config/server.properties
     * 创建topic：bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic lottery_invoice
     *
     * windows 启动指令
     * 启动zk：bin/zookeeper-server-start.bat -daemon config/zookeeper.properties
     * 启动kafka：bin/kafka-server-start.bat -daemon config/server.properties
     * 创建topic：bin/kafka-topics.bat --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic lottery_invoice
     *
     *
     */

    // 产生消息并发送到主题,通过回调函数检测发送消息的结果
    public ListenableFuture<SendResult<String, Object>> send(Object obi) {
        String msg = JSONUtil.toJsonStr(obi);
        log.info("准备发送的消息为：：：{}", msg);
        return kafkaTemplate.send(Constants.TOPIC_INVOICE, msg);
    }

}



